package com.ruoyi.oar.controller.app;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Response;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.ToolUtil;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oar.domain.dto.OarHouseDTO;
import com.ruoyi.oar.domain.query.OarHouseQuery;
import com.ruoyi.oar.service.OarHouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 房屋Controller
 * 
 * @author ruoyi
 * @date 2022-02-17
 */
@Api(tags = "APP房屋")
@RestController
@RequestMapping("/app/oar-houses")
public class AppHouseController extends BaseController
{
    @Autowired
    private OarHouseService oarHouseService;

    /**
     * 查询房屋列表
     */
    @ApiOperation("查询房屋列表")
    @GetMapping
    public TableDataInfo<OarHouseDTO> list(OarHouseQuery oarHouseQuery){
        logger.debug("查询房屋列表,查询条件oarHouseQuery={}",JSONObject.toJSONString(oarHouseQuery));
        startPage();
        //oarHouseQuery.setUserId(getUserId());
        return oarHouseService.findAll(oarHouseQuery);
    }

    /**
     * 导出房屋列表
     */
    @ApiOperation("导出房屋列表")
    @Log(title = "房屋", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OarHouseQuery oarHouseQuery){
        logger.debug("查询房屋列表,导出条件oarHouseQuery={}",JSONObject.toJSONString(oarHouseQuery));
        List<OarHouseDTO> list = oarHouseService.selectOarHouseList(oarHouseQuery);
        ExcelUtil<OarHouseDTO> util = new ExcelUtil<OarHouseDTO>(OarHouseDTO.class);
        return util.exportExcel(list, "房屋数据");
    }

    /**
     * 获取房屋详细信息
     */
    @ApiOperation("获取房屋详细信息")
    @GetMapping(value = "/{id}")
    public Response<OarHouseDTO> getInfo(@PathVariable("id") @ApiParam("房屋ID") String id) {
        logger.debug("获取房屋详细信息,id={}",id);
        checkId(id);
        return Response.success(oarHouseService.selectOarHouseById(id).orElseThrow(()->new ServiceException("无效的ID")));
    }

    /**
     * 新增房屋
     */
    @ApiOperation("新增房屋")
    @Log(title = "房屋", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit(interval=3000,message = "提交过于频繁，请稍后再试")
    public AjaxResult add(@RequestBody @Validated OarHouseDTO oarHouseDTO) {
        logger.debug("新增房屋,oarHouseDTO={}",JSONObject.toJSONString(oarHouseDTO));
        oarHouseDTO.setDataSources(0);
        return toAjax(oarHouseService.insertOarHouse(oarHouseDTO,0));
    }

    /**
     * 修改房屋
     */
    @ApiOperation("修改房屋")
    @Log(title = "房屋", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit(message = "提交过于频繁，请稍后再试")
    public AjaxResult edit(@RequestBody OarHouseDTO oarHouseDTO) {
        logger.debug("修改房屋,oarHouseDTO={}",JSONObject.toJSONString(oarHouseDTO));
        checkId(oarHouseDTO.getId());
        return toAjax(oarHouseService.updateOarHouse(oarHouseDTO,0));
    }

    /**
     * 删除房屋
     */
    @ApiOperation("删除房屋")
    @Log(title = "房屋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        logger.debug("删除房屋,ids={}",JSONObject.toJSONString(ids));
        Arrays.asList(ids).forEach(this::checkId);
        return toAjax(oarHouseService.deleteOarHouseByIds(ids));
    }

    /**
     * 查询当前用户房源
     */
    @ApiOperation("查询当前用户房源")
    @GetMapping(value = "/selByUserId")
    public TableDataInfo<OarHouseDTO> selByUserId(OarHouseQuery oarHouseQuery){
        startPage();
        //oarHouseQuery.setUserId(getLoginUser().getUserId());
        return oarHouseService.selByUserId(oarHouseQuery);
    }

    /**
     * 查询标准地址
     */
    @ApiOperation("查询标准地址")
    @GetMapping(value = "/getDzbm")
    public AjaxResult getDzbm(String addr){
        if (ToolUtil.isEmpty(addr)) {
            throw new ServiceException("addr不能为空");
        }
        return AjaxResult.success(null);
    }

    /**
     * 校验
     * @param id
     */
    private void checkId(String id) {
        Optional.of(id).orElseThrow(() -> new ServiceException("ID不能为空"));
        oarHouseService.selectOarHouseById(id).orElseThrow(() -> new ServiceException("无效的ID"));
    }
}
